package com.campus.controller;

import com.campus.dto.CourseConditionDto;
import com.campus.dto.TeachingCalling;
import com.campus.dto.TeachingCallingDetail;
import com.campus.model.Course;
import com.campus.model.TeachingEvaluation;
import com.campus.model.UnitCompletion;
import com.campus.service.*;

import com.campus.web.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/homework/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private TeachingEvaluationService teachingEvaluationService;

    @Autowired
    private CallingService callingService;

    @Autowired
    private TeachingCallingDetailService teachingCallingDetailService;

    @Autowired
    private CourseLearnUnitService courseLearnUnitService;

    @PostMapping("/create")
    public ResponseResult<String> createCourse(@RequestBody Course course) {
        boolean success = courseService.createCourse(course);
        return success
                ? ResponseResult.ok("Course created successfully")
                : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to create course");
    }

    @PostMapping("/evaluations/add")
    public ResponseResult<String> addEvaluation(@RequestBody TeachingEvaluation evaluation) {
        boolean isSuccess = teachingEvaluationService.addEvaluation(evaluation);
        return isSuccess
                ? ResponseResult.ok("Evaluation added successfully")
                : ResponseResult.error(HttpStatus.BAD_REQUEST, "Failed to add evaluation");
    }

    @GetMapping("/evaluations/{id}")
    public ResponseResult<TeachingEvaluation> getEvaluationById(@PathVariable Integer id) {
        TeachingEvaluation evaluation = teachingEvaluationService.getEvaluationById(id);
        return evaluation != null
                ? ResponseResult.ok(evaluation)
                : ResponseResult.error(HttpStatus.NOT_FOUND, "Evaluation not found");
    }

    @GetMapping("/evaluations/query")
    public ResponseResult<List<TeachingEvaluation>> getEvaluationsByCourseAndStudent(
            @RequestParam Integer courseId,
            @RequestParam Integer stuInClassId) {
        List<TeachingEvaluation> evaluations =
                teachingEvaluationService.getEvaluationsByCourseAndStudent(courseId, stuInClassId);
        return evaluations != null && !evaluations.isEmpty()
                ? ResponseResult.ok(evaluations)
                : ResponseResult.error(HttpStatus.NOT_FOUND, "No evaluations found");
    }

    @PostMapping("/query")
    public ResponseResult<List<Course>> queryCourses(@RequestBody CourseConditionDto condition) {
        List<Course> courses = courseService.getCoursesByCondition(condition);
        return courses != null && !courses.isEmpty()
                ? ResponseResult.ok(courses)
                : ResponseResult.error(HttpStatus.NOT_FOUND, "No courses found");
    }

    @PostMapping("/update")
    public ResponseResult<String> updateCourse(@RequestBody Course course) {
        boolean success = courseService.updateCourse(course);
        return success
                ? ResponseResult.ok("Course updated successfully")
                : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to update course");
    }

    @DeleteMapping("/delete/{id}")
    public ResponseResult<String> deleteCourse(@PathVariable int id) {
        boolean success = courseService.deleteCourse(id);
        return success
                ? ResponseResult.ok("Course deleted successfully")
                : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to delete course");
    }

    @GetMapping("/list")
    public ResponseResult<List<Course>> listCourses() {
        List<Course> courses = courseService.getAllCourses();
        return courses != null && !courses.isEmpty()
                ? ResponseResult.ok(courses)
                : ResponseResult.error(HttpStatus.NOT_FOUND, "No courses found");
    }

    @PostMapping("/calling/add")
    public ResponseResult<String> addCalling(@RequestBody TeachingCalling calling) {
        boolean success = callingService.addCalling(calling);
        return success
                ? ResponseResult.ok("Calling session added successfully")
                : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to add calling session");
    }

    @DeleteMapping("/calling/delete/{id}")
    public ResponseResult<String> deleteCalling(@PathVariable int id) {
        boolean success = callingService.deleteTeachingCallingById(id);
        return success
                ? ResponseResult.ok("Calling session deleted successfully")
                : ResponseResult.error(HttpStatus.NOT_FOUND, "Failed to delete calling session");
    }

    @PutMapping("/calling/update")
    public ResponseResult<String> updateCalling(@RequestBody TeachingCalling calling) {
        boolean success = callingService.updateTeachingCalling(calling);
        return success
                ? ResponseResult.ok("Calling session updated successfully")
                : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to update calling session");
    }

    @PostMapping("/calling-detail/add")
    public ResponseResult<String> addCallingDetail(@RequestBody TeachingCallingDetail detail) {
        boolean success = teachingCallingDetailService.addCallingDetail(detail);
        return success
                ? ResponseResult.ok("Calling detail added successfully")
                : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to add calling detail");
    }

    @DeleteMapping("/calling-detail/delete/{id}")
    public ResponseResult<String> deleteCallingDetail(@PathVariable int id) {
        boolean success = teachingCallingDetailService.deleteCallingDetailById(id);
        return success
                ? ResponseResult.ok("Calling detail deleted successfully")
                : ResponseResult.error(HttpStatus.NOT_FOUND, "Failed to delete calling detail");
    }

    @PutMapping("/calling-detail/update")
    public ResponseResult<String> updateCallingDetail(@RequestBody TeachingCallingDetail detail) {
        boolean success = teachingCallingDetailService.updateCallingDetail(detail);
        return success
                ? ResponseResult.ok("Calling detail updated successfully")
                : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to update calling detail");
    }

    @GetMapping("/calling/list")
    public ResponseResult<List<TeachingCalling>> listCallingSessions() {
        List<TeachingCalling> callings = callingService.getAllCallings();
        return callings != null && !callings.isEmpty()
                ? ResponseResult.ok(callings)
                : ResponseResult.error(HttpStatus.NOT_FOUND, "No calling sessions found");
    }

    @GetMapping("/calling-detail/list")
    public ResponseResult<List<TeachingCallingDetail>> listCallingDetails() {
        List<TeachingCallingDetail> details = teachingCallingDetailService.getAllCallingDetails();
        return details != null && !details.isEmpty()
                ? ResponseResult.ok(details)
                : ResponseResult.error(HttpStatus.NOT_FOUND, "No calling details found");
    }

    @GetMapping("/units/completion")
    public ResponseResult<List<UnitCompletion>> getAllUnitsCompletion() {
        // 获取所有学习单元的完成度
        List<UnitCompletion> unitCompletionRates = courseLearnUnitService.getAllUnitsCompletion();
        return unitCompletionRates != null && !unitCompletionRates.isEmpty()
                ? ResponseResult.ok(unitCompletionRates)
                : ResponseResult.error(HttpStatus.NOT_FOUND, "No unit completion data found");
    }

}
